package cn.hs.dao.impl;

import cn.hs.dao.ShopDao;
import cn.hs.entity.Shop;
import cn.hs.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ShopDaoImpl implements ShopDao {
    @Override
    public int insertShop(Shop shop) {
        String sql="insert into shop(shopName,shopDescribe,shopImage,shopPhone,shopProvince,shopCity,shopDistrict,shopAddress,shopLongitude,shopLatitude,addTime)values(?,?,?,?,?,?,?,?,?,?,now())";
        Object object[]={
                shop.getShopName(),
                shop.getShopDescribe(),
                shop.getShopImage(),
                shop.getShopPhone(),
                shop.getShopProvince(),
                shop.getShopCity(),
                shop.getShopDistrict(),
                shop.getShopAddress(),
                shop.getShopLongitude(),
                shop.getShopLatitude(),

        };
        int row = JDBCDruidUtil.update(sql,object);
        return row;
    }

    @Override
    public int updateShop(Shop shop){
        String sql = "UPDATE shop SET shopName = ?, shopDescribe = ?, shopImage = ?, shopPhone = ?, shopProvince = ?, shopCity = ?, shopDistrict = ?, shopAddress = ?, shopLongitude = ?, shopLatitude = ?  WHERE shopId = ?";
        Object object[]={
                shop.getShopName(),
                shop.getShopDescribe(),
                shop.getShopImage(),
                shop.getShopPhone(),
                shop.getShopProvince(),
                shop.getShopCity(),
                shop.getShopDistrict(),
                shop.getShopAddress(),
                shop.getShopLongitude(),
                shop.getShopLatitude(),
                shop.getShopId()

        };
        int row = JDBCDruidUtil.update(sql,object);
        return row;
    }

    @Override
    public List<Shop> selectShop(Map<String, String> map) {
        List<Shop> list = new ArrayList<>();
        //动态sql
        StringBuffer sb = new StringBuffer("select * from shop where 1=1 ");
        //动态参数
        List param = new ArrayList();
        String shopName = map.get("keyword");
        if (shopName!=null && !"".equals(shopName)){
            sb.append(" and shopName like ? ");
            param.add("%"+shopName+"%");
        }
        //判断手机号是否存在
        String phone = map.get("phone");
        if (phone!=null && !"".equals(phone)){
            sb.append(" and shopPhone = ? ");
            param.add(phone);
        }
        //判断日期是否存在
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if (startTime!=null && !"".equals(startTime)){
            sb.append(" and addTime between ? and ? ");
            param.add(startTime);
            param.add(endTime);
        }
        //分页处理
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize = Integer.valueOf(map.get("pageSize"));
        sb.append(" limit ?,? ");
        param.add((pageNum-1)*pageSize);
        param.add(pageSize);
        String sql = sb.toString();
        Object[] objects = param.toArray();
        System.out.println(sql);
        ResultSet resultSet = JDBCDruidUtil.query(sql, objects);
        try {
            while (resultSet.next()){
                Shop shop = new Shop(
                        resultSet.getInt(1),
                        resultSet.getString(2),
                        resultSet.getString(3),
                        resultSet.getString(4),
                        resultSet.getString(5),
                        resultSet.getString(6),
                        resultSet.getString(7),
                        resultSet.getString(8),
                        resultSet.getString(9),
                        resultSet.getString(10),
                        resultSet.getString(11),
                        resultSet.getInt(12),
                        resultSet.getDate(13),
                        resultSet.getInt(14),
                        resultSet.getString(15),
                        resultSet.getString(16),
                        resultSet.getString(17),
                        resultSet.getInt(18),
                        resultSet.getString(19),
                        resultSet.getString(20),
                        resultSet.getString(21),
                        resultSet.getInt(22),
                        resultSet.getString(23),
                        resultSet.getString(24)
                );

                list.add(shop);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCDruidUtil.close();
        }
        return list;
    }

    @Override
    public int selectShopCount(Map<String, String> map) {
        int count = 0;
        //动态sql
        StringBuffer sb = new StringBuffer("select count(*) from shop where 1=1 ");
        //动态参数
        List param = new ArrayList();
        //判断名称是否存在
        String shopName = map.get("keyword");
        if (shopName!=null && !"".equals(shopName)){
            sb.append(" and shopName like ? ");
            param.add("%"+shopName+"%");
        }
        //判断手机号是否存在
        String phone = map.get("phone");
        if (phone!=null && !"".equals(phone)){
            sb.append(" and shopPhone = ? ");
            param.add(phone);
        }
        //判断日期是否存在
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if (startTime!=null && !"".equals(startTime)){
            sb.append(" and addTime between ? and ? ");
            param.add(startTime);
            param.add(endTime);
        }
        String sql = sb.toString();
        Object[] objects = param.toArray();
        ResultSet resultSet = JDBCDruidUtil.query(sql, objects);
        try {
           if (resultSet.next()){
               count = resultSet.getInt(1);
           }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCDruidUtil.close();
        }
        return count;
    }
}
